﻿using Common;

using Dapr.Actors;
using Dapr.Actors.Client;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace FrontEnd.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ActorsClientController : ControllerBase
    {
        [HttpGet("{orderId}")]
        public async Task<ActionResult> ApproveAsync(string orderId)
        {
            var actorId = new ActorId("actorprifix-" + orderId);
            var proxy = ActorProxy.Create<IWorkflowActor>(actorId, "WorkflowActor");

            return Ok(await proxy.Approve());
        }

        [HttpGet("timer/{orderId}")]
        public async Task<ActionResult> TimerAsync(string orderId)
        {
            var actorId = new ActorId("actorprifix-" + orderId);
            var proxy = ActorProxy.Create<IWorkflowActor>(actorId, "WorkflowActor");
            await proxy.RegisterTimer();
            return Ok("done");
        }

        [HttpGet("unregist/timer/{orderId}")]
        public async Task<ActionResult> UnregistTimerAsync(string orderId)
        {
            var actorId = new ActorId("actorprifix-" + orderId);
            var proxy = ActorProxy.Create<IWorkflowActor>(actorId, "WorkflowActor");
            await proxy.UnregisterTimer();
            return Ok("done");
        }

        [HttpGet("reminder/{orderId}")]
        public async Task<ActionResult> ReminderAsync(string orderId)
        {
            var actorId = new ActorId("actorprifix-" + orderId);
            var proxy = ActorProxy.Create<IWorkflowActor>(actorId, "WorkflowActor");
            await proxy.RegisterReminder();
            return Ok("done");
        }

        [HttpGet("unregist/reminder/{orderId}")]
        public async Task<ActionResult> UnregistReminderAsync(string orderId)
        {
            var actorId = new ActorId("actorprifix-" + orderId);
            var proxy = ActorProxy.Create<IWorkflowActor>(actorId, "WorkflowActor");
            await proxy.UnregisterReminder();
            return Ok("done");
        }
    }
}